package com.test.springCloud.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String PREFIX = "http://localhost:8072";//本项目提供的配置port,也就是服务器提供者的端口号

    /**
     * 用户注册
     * @param req
     * @return
     */
    @PostMapping("/client/lxf/register")
    public String register(@RequestBody JSONObject req) {

        String mobile = req.getString("userName");
        String password = req.getString("password");
        String nickName = req.getString("nickName");//昵称
        Integer age = req.getInteger("Age");
        String sex = req.getString("Sex");
        String address = req.getString("Address");
        String avatar = req.getString("Avatar");
        Integer account = req.getInteger("Account");

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);

        //设置请求体
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("userName", mobile);
        map.put("password", password);
        map.put("nickName",nickName);
        map.put("age",age);
        map.put("sex",sex);
        map.put("address",address);
        map.put("avatar",avatar);
        map.put("account",account);
        HttpEntity<HashMap<String,Object>> request = new HttpEntity<HashMap<String,Object>>(map, headers);

        //发起请求
        ResponseEntity<String> response = restTemplate.postForEntity(PREFIX+"/lxf/res/register", request, String.class);
        return response.getBody();
    }

    /**
     * 用户登录
     * @param req
     * @return
     */
    @PostMapping("/client/lxf/login")
    public String login(@RequestBody JSONObject req){

        String userName = req.getString("userName");
        String password = req.getString("password");

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);

        //设置请求体
        HashMap<String, String> map = new HashMap<String,String>();
        map.put("userName", userName);
        map.put("password", password);
        HttpEntity<HashMap<String,String>> request = new HttpEntity<HashMap<String,String>>(map, headers);

        //发起请求
        ResponseEntity<String> response = restTemplate.postForEntity(PREFIX+"/lxf/userLogin", request, String.class);

        return response.getBody();
    }


    /**
     * 发送短信验证
     * @param req
     */
    @PostMapping("/client/lxf/send")
    public void send(@RequestBody JSONObject req) {
        String userName = req.getString("userName");
//		String password = req.getString("password");

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);

        //设置请求体
        HashMap<String, String>map = new HashMap<String,String>();
        map.put("userName", userName);
//        map.put("password", password);
        HttpEntity<HashMap<String,String>> request = new HttpEntity<HashMap<String,String>>(map, headers);

        //发起请求
        restTemplate.postForEntity(PREFIX+"/lxf/s/send", request, String.class);
    }

    /**
     * 退出文件
     */
    @PostMapping("/client/lxf/loginOut")
    public String loginOut(HttpServletRequest req){

        String token = req.getHeader("token");
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        headers.add(HttpHeaders.AUTHORIZATION,"token"+token);
        //设置请求体
        HttpEntity<HashMap<String,Integer>> request = new HttpEntity<HashMap<String,Integer>>(null, headers);

        //发起请求
        ResponseEntity<String> response = restTemplate.postForEntity(PREFIX+"/lxf/lo/loginOut", request, String.class);

        return response.getBody();
    }
}
